/**
 * @author wxn
 * @since 12-5-24 4:13
 */
package com.sihai.ehr.web.action.organization;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

@Results({@Result(name = "list", location = "list.jsp")})
public class ListAction extends EHRAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672240877L;

    @BeanInject
    private OrganizationService organizationService;

    private List<Organization> organizations;

    @Action("/organization/list")
    @Override
    public String execute() throws Exception {
        organizations = organizationService.findChildren(null, true, null, null);
        if(organizations != null && organizations.size() > 0 &&  getCurrentUserId() > 0){
        	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
        	Iterator<Organization> it = organizations.iterator();
        	while(it.hasNext()){
        		Organization o = it.next();
        		if(o.getId() != topOrgId){
        			it.remove();
        		}
        	}
        }
        return "list";
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }
}
